#include<bits/stdc++.h>
#define int long long
#define N 1000005
using namespace std;
inline int read(){
    int x=0,f=1;char ch=getchar();
    while(!isdigit(ch)){if(ch=='-') f=-1;ch=getchar();}
    while(isdigit(ch)){x=(x<<3)+(x<<1)+(ch^48);ch=getchar();}
    return x*f;
}
int T;
int n;
int a[N],b[N];
int len,c[N];
int ans;
void sol(){
    n=read();
    for(int i=1;i<=n;i++) a[i]=read();
    for(int i=1;i<=n;i++) b[i]=read();
    ans=0;len=0;
    for(int i=1;i<=n;i++){
        if(a[i]==0&&b[i]!=0){printf("-1\n");return;}
        if(b[i]==0){
            if(b[i-1]==0) ans+=max(a[i]-a[i-1],0ll);
            else ans+=a[i];
        }
        else c[++len]=i;
    }
    for(int i=1;i<=len;i++){
        int id=c[i],lst=c[i-1];
        if(a[id]>b[id]) ans+=max(a[id]-b[id]-(a[lst]>b[lst])*(a[lst]-b[lst]),0ll);
        if(a[id]<b[id]) ans+=max(b[id]-a[id]-(b[lst]>a[lst])*(b[lst]-a[lst]),0ll);
    }
    printf("%lld\n",ans);
}
signed main(){
    freopen("game.in","r",stdin);
    freopen("game.out","w",stdout);
    T=read();
    while(T--) sol();
    return 0;
}